package com.sc.nft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sc.nft.dao.MallUserActivitySummaryDao;
import com.sc.nft.entity.MallUserActivitySummary;
import com.sc.nft.service.MallUserActivitySummaryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * IP商城用户活动兑换表服务接口实现
 *
 * @author zxy
 * @since 2023-05-10 16:59:01
 * @description IP商城
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class MallUserActivitySummaryServiceImpl implements MallUserActivitySummaryService {

    private final MallUserActivitySummaryDao mallUserActivitySummaryDao;

    /**
     * 累计消费金额
     * @param userId
     * @param consumptionAmount
     */
    @Override
    public void updateTotalConsumptionAmount(Long userId, BigDecimal consumptionAmount) {
        MallUserActivitySummary mallUserActivitySummary = this.get(userId);
        if(mallUserActivitySummary != null){
            mallUserActivitySummaryDao.updateTotalConsumptionAmount(userId, consumptionAmount);
        }else{
            this.save(userId,consumptionAmount,BigDecimal.ZERO);
        }
    }

    /**
     * 累计兑换金额
     * @param userId
     * @param exchangeAmount
     */
    @Override
    public void updateTotalExchangeAmount(Long userId, BigDecimal exchangeAmount) {
        MallUserActivitySummary mallUserActivitySummary = this.get(userId);
        if(mallUserActivitySummary != null) {
            mallUserActivitySummaryDao.updateTotalExchangeAmount(userId, exchangeAmount);
        }else {
            this.save(userId,BigDecimal.ZERO,exchangeAmount);
        }
    }

    /**
     * 累计兑换金额
     * @param userId
     * @param totalConsumptionAmount
     */
    @Override
    public void save(Long userId, BigDecimal totalConsumptionAmount, BigDecimal totalExchangeAmount) {
        MallUserActivitySummary mallUserActivitySummary = new MallUserActivitySummary();
        mallUserActivitySummary.setUserId(userId);
        mallUserActivitySummary.setTotalConsumptionAmount(totalConsumptionAmount);
        mallUserActivitySummary.setTotalExchangeAmount(totalExchangeAmount);
        mallUserActivitySummaryDao.save(mallUserActivitySummary);
    }

    /**
     * 通过userId获取统计记录
     * @param userId
     * @return
     */
    @Override
    public MallUserActivitySummary get(Long userId){
        LambdaQueryWrapper<MallUserActivitySummary> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallUserActivitySummary::getUserId, userId);
        wrapper.last("limit 1");
        return mallUserActivitySummaryDao.getOne(wrapper);
    }
}